import os, sys
import cPickle, random, copy
from sets import Set

if __name__ == "__main__":
    problems = []
    necessary_problems = []
    words = []
    try:
        f = open("setting.env")
        version = cPickle.load(f)
        if version == 1.0:
            problems = cPickle.load(f)
            fo.close()
        else:
            print "Version error..."
            f.close()
            sys.exit(1)

    except IOError:
        f = open("antonym.txt")
        for line in f:
            line = line.strip()
            a = line.split("\t")
            if len(a) < 2 and len(a) != 0:
                print a
                sys.exit(1)

            if len(a) == 0:
                continue

            temp_left_words = a[0].split(",")
            left_words = []
            for word in temp_left_words:
                left_words.append(word.strip())

            temp_right_words = a[1].split(",")
            right_words = []
            for word in temp_right_words:
                right_words.append(word.strip())

            if len(a) == 2:
                problems.append((a,left_words,right_words))
            if len(a) > 2:
                necessary_problems.append((a,left_words,right_words))

            words.extend(left_words)
            words.extend(right_words)
    except:
        f.close()
        print "error...."
        sys.exit(1)
    temp_words = Set(words)
    words = []
    for word in temp_words:
        words.append(word)
        print word

    f_prob = open("anto_prob.txt", "w")
    f_answ = open("anto_answ.txt", "w")

    random.shuffle(problems)
    count = 1
    temp_stack = []
    for problem in problems:
        if len(problem) != 3:
            print "problem error: %s"%str(problem)
            continue
        random.shuffle(problem[1])
        random.shuffle(problem[2])
        
        if random.random() > 0.5:
            front = 1
            back = 2
        else:
            front = 2
            back = 1
            
        temp_stack.append(problem[back][0])
        f_prob.write("%d. %s  " % (count, problem[front][0].upper()))
        f_answ.write("%d. %s ↔ %s\n"%(count, problem[front][0].upper(),problem[back][0].upper()))
        
        if count % 5 == 0:
            random.shuffle(words)
            for word in words:
                try:
                    temp_stack.index(word)
                except:
                    temp_stack.append(word)

                if len(temp_stack) >= 10:
                    break

            random.shuffle(temp_stack)
            f_prob.write("\n| ")
            for word in temp_stack:
                f_prob.write("%s | "%word.upper())
            temp_stack = []
            f_prob.write("\n\n")

        count += 1;

        if count > 200:
            break
    
    random.shuffle(necessary_problems)
    for problem in necessary_problems:
        if len(problem) != 3:
            print "problem error: %s"%str(problem)
            continue
        random.shuffle(problem[1])
        random.shuffle(problem[2])
        
        if random.random() > 0.5:
            front = 1
            back = 2
        else:
            front = 2
            back = 1
            
        temp_stack.append(problem[back][0])
        f_prob.write("%d. %s  " % (count, problem[front][0].upper()))
        f_answ.write("%d. %s ↔ %s\n"%(count, problem[front][0].upper(),problem[back][0].upper()))
        
        if count % 5 == 0:
            random.shuffle(words)
            for word in words:
                try:
                    temp_stack.index(word)
                except:
                    temp_stack.append(word)

                if len(temp_stack) >= 10:
                    break

            random.shuffle(temp_stack)
            f_prob.write("\n| ")
            for word in temp_stack:
                f_prob.write("%s | "%word.upper())
            temp_stack = []
            f_prob.write("\n\n")

        count += 1;

    if count % 5 != 1:
        random.shuffle(words)
        for word in words:
            try:
                temp_stack.index(word)
            except:
                temp_stack.append(word)
                
            if len(temp_stack) >= 10:
                break

        random.shuffle(temp_stack)
        f_prob.write("\n| ")
        for word in temp_stack:
            f_prob.write("%s | "%word.upper())
        f_prob.write("\n\n")
            
    f_prob.close()
    f_answ.close()
